package com.hw.pusher;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;

import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.agconnect.config.LazyInputStream;
import com.hw.pusher.utils.DeviceUtils;
import com.xiaomi.mipush.sdk.MiPushClient;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import io.flutter.app.FlutterApplication;

public class HWPushApplication extends FlutterApplication {

    // 小米push　设置
    private static final String APP_ID = "2882303761518364667";
    private static final String APP_KEY = "5111836496667";

    // 此TAG在adb logcat中检索自己所需要的信息， 只需在命令行终端输入 adb logcat | grep
    // com.xiaomi.mipushdemo


    @Override
    public void onCreate() {
        super.onCreate();
        // 注册push服务，注册成功后会向DemoMessageReceiver发送广播
        // 可以从DemoMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息
        if (shouldInit()) {
            MiPushClient.registerPush(this, APP_ID, APP_KEY);
        }

        DeviceUtils.getDeviceInfo();
    }

    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }
//    public static void reInitPush(Context ctx) {
//        MiPushClient.registerPush(ctx.getApplicationContext(), APP_ID, APP_KEY);
//    }


    @Override
    protected void attachBaseContext(Context context) {

        super.attachBaseContext(context);

        AGConnectServicesConfig config = AGConnectServicesConfig.fromContext(context);

        config.overlayWith(new LazyInputStream(context) {

            public InputStream get(Context context) {

                try {

                    return context.getAssets().open("agconnect-services.json");

                } catch (IOException e) {

                    return null;

                }

            }

        });

    }
}
